package cn.ekuma.odbo.mobile.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import cn.ekuma.odbo.mobile.R
import cn.ekuma.odbo.mobile.base.I_BrowsableEditableData
import com.openbravo.data.user.DirtyListener


abstract class  AbstractOdboBrowseAbleEditorFragment<T>: Fragment() , DirtyListener{

    private lateinit var editViewGroup:View
    protected lateinit var del:Button
    protected lateinit var save:Button
    protected lateinit var cancel:Button

    protected lateinit var first:Button
    protected lateinit var end:Button
    protected lateinit var prev:Button
    protected lateinit var next:Button

    protected lateinit var frameLayout: FrameLayout

    protected lateinit var browser: I_BrowsableEditableData<T>

    protected lateinit var editorRecordHolder: EditorRecordHolder<T>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        browser=buildBrowAble()
        //browser.addStateListener { editorRecordHolder.refresh(browser.currentElement) }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        editViewGroup=inflater.inflate(R.layout.odbc_edit_container,container,false)
        del=editViewGroup.findViewById(R.id.delete)
        del.setOnClickListener {

        }
        save=editViewGroup.findViewById(R.id.save)
        save.setOnClickListener {

        }
        cancel=editViewGroup.findViewById(R.id.cancel)
        cancel.setOnClickListener {

        }

        first=editViewGroup.findViewById(R.id.first)
        first.setOnClickListener { browser.moveFirst() }
        end=editViewGroup.findViewById(R.id.end)
        end.setOnClickListener { browser.moveLast() }
        prev=editViewGroup.findViewById(R.id.prev)
        prev.setOnClickListener { browser.movePrev() }
        next=editViewGroup.findViewById(R.id.next)
        next.setOnClickListener { browser.moveNext() }

        frameLayout=editViewGroup.findViewById(R.id.frameLayout)

        editorRecordHolder= onCreateEditorRecordHolder()
        editorRecordHolder.setDirtyListener(this)

        frameLayout.addView(editorRecordHolder.getEditorViewPanel(inflater,frameLayout))

        return editViewGroup
    }

    abstract fun onCreateEditorRecordHolder():EditorRecordHolder<T>

    abstract fun buildBrowAble():I_BrowsableEditableData<T>

    override fun changedDirty(bDirty: Boolean) {
        save.isClickable=bDirty
        cancel.isClickable=bDirty
    }

    fun updateNavButtonState(){
        //first.isEnabled=
    }

}